package ycl.important.controller.hovel;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ycl.important.common.ApiResult;
import ycl.important.common.constants.FileConstant;
import ycl.important.common.models.res.hovel.UploadRes;
import ycl.important.common.models.res.hovel.UploadVideoRes;
import ycl.important.service.hovel.UploadService;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.File;

/**
 * 上传
 *
 * @author: YCL
 * @date: 2021-09-01 23:23:10
 */
@RestController
@RequestMapping("/hovel/upload")
@Api(tags = "上传")
@ApiSort(99)
@Validated
public class UploadController {

	@Resource
	private UploadService uploadService;

	@PostMapping(value = "/video")
	@ApiOperation("上传视频")
	@ApiOperationSupport(order = 1)
	@ApiImplicitParam(value = "上传视频",
			name = FileConstant.UPLOAD_TYPE_FILE,
			required = FileConstant.UPLOAD_REQUIRED,
			dataTypeClass = File.class,
			paramType = FileConstant.UPLOAD_PARAM_TYPE)
	public ApiResult<UploadVideoRes> video(
			@RequestParam("file")
			@NotNull(message = "文件不能为空")
					MultipartFile file) {
		UploadVideoRes vo = uploadService.video(file, "video");
		return ApiResult.success(vo);
	}


	@PostMapping(value = "/images")
	@ApiOperation("上传图片")
	@ApiOperationSupport(order = 11)
	@ApiImplicitParam(value = "上传图片",
			name = FileConstant.UPLOAD_TYPE_FILE,
			required = FileConstant.UPLOAD_REQUIRED,
			dataTypeClass = File.class,
			paramType = FileConstant.UPLOAD_PARAM_TYPE)
	public ApiResult<UploadRes> images(
			@RequestParam("file")
			@NotNull(message = "文件不能为空")
					MultipartFile file) {
		UploadRes vo = uploadService.images(file, "images");
		return ApiResult.success(vo);
	}
}
